package com.xianshijian.oauth.resource.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.xianshijian.oauth.common.entity.OAuth;
import com.xianshijian.oauth.common.entity.OAuthResult;

/**
 * 资源服务Service
 * 
 * @author lzj
 *
 */
@Service
public class ResourceService {
	
	
	@Value("${oauth.userinfo.url}")
    private String userInfoUrl;

    /**
     * 调用获取用户信息接口
     * @param accessToken
     * @return
     */
    public OAuthResult findUserByAccessToken(String accessToken) {
		RestTemplate restTemplate = new RestTemplate();
		return restTemplate.getForObject(userInfoUrl + "userinfo/{accessToken}", OAuthResult.class, accessToken); 
    }
    
    /**
     * 验证access_token是否有效
     * @param accessToken
     * @return
     */
    public boolean checkAccessToken(String accessToken) {
    	RestTemplate restTemplate = new RestTemplate();
    	OAuthResult oAuthResult = restTemplate.getForObject(userInfoUrl + "checkAccessToken/{accessToken}", OAuthResult.class, accessToken); 
    	if(oAuthResult.getStatus()==OAuth.StatusCode.OK){
    		return true;
    	}
    	return false;
    }
    
    


}
